package com.demo.steammanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.steammanagement.common.CommonUtils;
import com.demo.steammanagement.common.ResultMsg;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.convert.GameConvert;
import com.demo.steammanagement.model.entity.Games;
import com.demo.steammanagement.model.vo.GamesVO;
import com.demo.steammanagement.service.IGamesService;
import com.demo.steammanagement.mapper.GamesMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
* @author Administrator
* @description 针对表【games(存储游戏的基本信息)】的数据库操作Service实现
* @createDate 2024-12-09 15:45:45
*/
@Service
public class IGamesServiceImpl extends ServiceImpl<GamesMapper, Games>
    implements IGamesService {

    @Autowired
    private CommonUtils commonUtils;

    @Override
    public Boolean customSaveOrUpdate(GamesVO vo) throws IOException {
        validateGames(vo);
        if (ObjectUtils.isNotEmpty(vo.getImg())) {
            String imgUrl  = commonUtils.getUploadUrl(vo.getImg());
            vo.setImageUrl(imgUrl);
        }
        return this.saveOrUpdate(GameConvert.INSTANCE.convertVOToEntity(vo));
    }

    private void validateGames(GamesVO vo) {
        if (ObjectUtils.isEmpty(vo)) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        if (StringUtils.isBlank(vo.getName())) {
            throw new BizException(ResultMsg.NAME_IS_EMPTY.getMsg());
        }
    }
}




